$(function () {
    $("#login").click(function(){
        //清除提示
        $("#count_span").html("");
        $("#password_span").html("");

        //获取请求参数
        var name = $("#count").val().trim();
        var password = $("#password").val().trim();
        //检查参数格式
        var ok = true;
        if(name == ""){
            ok = false;
            $("#count_span").html("用户名为空");
        }
        if(password == ""){
            ok = false;
            $("#password_span").html("密码为空");
        }
        //发送ajax请求
        if(ok){
            $.ajax({
                url: "http://localhost/user/login.do",
                type: "post",
                data: {"name":name,"password":password},
                dataType: "json",
                success:function(result){
                    console.log(result);
                    if(result.status == 0){//登陆成功
                        var userId = result.data;//获取用户id存入cookie
                        addCookie("userId",userId,0.5);
                        window.location.href = "edit.html";
                    }else if(result.status == 1){
                        $("#count_span").html(result.msg);

                    }else if(result.status == 2){
                        $("#password_span").html(result.msg);
                    }
                },
                error:function(XMLHttpRequest,textStatus,errorThrown){
                    //console.log(XMLHttpRequest.status);
                    //console.log(textStatus.readyState);
                    //console.log(errorThrown);
                    alert("登陆失败，请重试")
                }
            })
        }
    });
})